package com.lz.controller;

import com.lz.entity.OrderMasterQuery;
import com.lz.enums.ResultEnum;
import com.lz.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 卖家端订单
 * Created by Administrator on 2018/9/3 0003.
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单列表
     * @param page 第几页
     * @param size 一页多少数据
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "size",defaultValue = "10")Integer size,
                       Model model){

        PageRequest pageRequest = new PageRequest(page - 1,size);
        Page<OrderMasterQuery> list = orderService.findList(pageRequest);
        model.addAttribute("orderPage",list);
        model.addAttribute("size",size);
        return "order/thymeleaf/list";
        //return "order/freemarker/list";
    }

    /**
     * 取消订单
     * @param orderId 订单编号
     * @param model
     * @return
     */
    @GetMapping("/cancel")
    public String cancel(@RequestParam("orderId")String orderId,
                         Model model){
        try{
            OrderMasterQuery one = orderService.findOne(orderId);
            orderService.cancelOrder(one);
        } catch (Exception e){
            log.error("【卖家取消订单】发生异常{}",e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url","/seller/order/list");
            return "common/error";
        }
        model.addAttribute("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        model.addAttribute("url","/seller/order/list");
        return "common/success";
    }

    /**
     * 订单详情
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/detail")
    public String detail(@RequestParam("orderId")String orderId,
                         Model model){
        OrderMasterQuery orderMasterQuery = new OrderMasterQuery();
        try {
            orderMasterQuery = orderService.findOne(orderId);
        } catch (Exception e) {
            log.error("【卖家查询订单详情】发生异常{}",e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url","/seller/order/list");
            return "common/error";
        }
        model.addAttribute("orderMasterQuery",orderMasterQuery);
        return "order/thymeleaf/detail";
    }

    /**
     * 完结订单
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/finish")
    public String finish(@RequestParam("orderId")String orderId,
                         Model model){
        OrderMasterQuery orderMasterQuery = new OrderMasterQuery();
        try {
            orderMasterQuery = orderService.findOne(orderId);
            orderService.finishOrder(orderMasterQuery);
        } catch (Exception e) {
            log.error("【卖家完结订单】发生异常{}",e);
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url","/seller/order/list");
            return "common/error";
        }
        model.addAttribute("msg",ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        model.addAttribute("url","/seller/order/list");
        return "common/success";
    }
}
